<?php
namespace WebAdmin\Controller;
use WebAdmin\Controller\BaseController;
class NewsController extends BaseController {

	//ThinkPHP的构造函数
	public function _initialize() {
		//自动加载的类
		new \Org\Admin_autoload();
	}

    public function index(){
    	$this->assign('page_title','资讯列表');
    	$NewsModel = D("News");
    	//$items = $NewsModel->where("ntype=2")->select();
		$items = $NewsModel->select();
    	$this->assign('volist',$items);
    	
    	//$this->assign('')
        $this->display();
    }
    
    public function add(){
    	if(!empty($_POST)) {
			$NewsModel = D("News");
			$_POST['newsdt'] = date('Y-m-d H:i:s');
			$_POST['is_show'] = 1;
			$_POST['ntype'] = 2;

			//增加大小两张缩略图 by hyy 20170223
            if(!empty($_POST['images_url'])){
                $base_url=dirname(dirname(dirname(dirname(__FILE__)))).'/www/';
				$dir_name=date("Ymd"); //放到当天文件夹中
				$base_img_url=$base_url.'Public/Uploads/News/'.$dir_name.'/';
				if (!file_exists($base_img_url)) {
					mkdir($base_img_url);
				}
				//echo $base_img_url;exit;
				//生成缩略图
				$image_url= $base_url.$_POST['images_url'];
				$image= new \Think\Image();
				$image->open($image_url);
				$small_thumb=date("YmdHis").'_'.rand(10000, 99999).'_small'.'.jpg';
				$large_thumb=date("YmdHis").'_'.rand(10000, 99999).'_large'.'.jpg';
				$image->thumb(135,90)->save($base_img_url.$small_thumb);
				$image->open($image_url);
				$image->thumb(150,100)->save($base_img_url.$large_thumb);
				$_POST['small_thumb']= '/Public/Uploads/News/'.$dir_name.'/'.$small_thumb;
				$_POST['large_thumb']= '/Public/Uploads/News/'.$dir_name.'/'.$large_thumb;
				//unset($_POST['images_url']);
            }
    		
    		$data = $NewsModel->create($_POST);
    		$insert_id = $NewsModel->add($data);
    		if($insert_id){
    			
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>window.location.href='".__APP__."/index.php/News/';</script>";exit;
    		}
    	}else{
    	    $CateModel = D("NewsCategory");
    	    $category_list = $CateModel->where("ntype=2")->select();
    		$this->assign('category_list',$category_list);
    	    $this->display();
    	}
    }
    
    public function getData(){
    	$CateModel = D("NewsCategory");
    	//$category_list = $CateModel->where('ntype=2')->select();
		$category_list = $CateModel->select();
    	$categories = array();
    	foreach($category_list as $v){
    		$categories[$v['id']] = $v['name'];
    	}
    	$DataModel = D("News");
    	$items = array();
    	$page = (int)$_GET['page'];
    	$items['page'] = $page>0 ? $page :1;
    	$items['records'] = $DataModel->count();
    	$limit = $_GET['rows'] ? (int)$_GET['rows']: 20;
    	$items['total'] = ceil($items['records']/$limit);
    	$list = $DataModel->page($page)->order('descno asc,id desc')->limit($limit)->select();
    	for($i=0;$i<count($list);$i++){
    		$list[$i]['category'] = $categories[$list[$i]['category_id']];
			$list[$i]['large_thumb'] = 'http://'.FRONT_SITE_NAME.$list[$i]['large_thumb'];
    	}
    	$items['rows'] = $list;
    	$this->ajaxReturn($items);
    }
    
    
    public function edit(){
    	$DataModel = D("News");
    	if(!empty($_POST)) {
    		$_POST['newsdt'] = date('Y-m-d H:i:s');
			//增加缩略图 by lijianjun 20170215  hyy 2.23  裁大小两张图
			//增加从相册选择缩略图 by huangcan 20170222
			if(!empty($_POST['images_url'])){
				//echo $_POST['images_url'];exit;
                $base_url=dirname(dirname(dirname(dirname(__FILE__)))).'/www/';
				$dir_name=date("Ymd"); //放到当天文件夹中
				//$dir_name=20170314;
                $base_img_url=$base_url.'Public/Uploads/News/'.$dir_name.'/';
				if (!file_exists($base_img_url)) {
					mkdir($base_img_url);
				}
				//echo $base_img_url;exit;
				//生成缩略图
                $image_url= $base_url.$_POST['images_url'];
                $image= new \Think\Image();
                $image->open($image_url);
                $small_thumb=date("YmdHis").'_'.rand(10000, 99999).'_small'.'.jpg';
				$large_thumb=date("YmdHis").'_'.rand(10000, 99999).'_large'.'.jpg';
				$image->thumb(135,90)->save($base_img_url.$small_thumb);
				$image->open($image_url);
				$image->thumb(150,100)->save($base_img_url.$large_thumb);
				$_POST['small_thumb']= '/Public/Uploads/News/'.$dir_name.'/'.$small_thumb;
				$_POST['large_thumb']= '/Public/Uploads/News/'.$dir_name.'/'.$large_thumb;
				//unset($_POST['images_url']);
			}
    		$DataModel->where("id=".(int)$_POST['id'])->save($_POST);
    		header("Content-type: text/html; charset=utf-8");
    		echo "<script>window.location.href='".__APP__."/index.php/News/';</script>";exit;
    	}else{
    		$item = $DataModel->where("id=".(int)$_GET['id'])->select();
    		$this->assign('vo',$item[0]);
    		$CateModel = D("NewsCategory");
    		$category_list = $CateModel->where('ntype=2')->select();
    		$this->assign('category_list',$category_list);
    		$this->display();
    	}
    }
    
    public function delete(){
    	if(!empty($_POST)) {
    		$DataModel = D("News");
    		$id = $_POST['id'];
    		$item =  $DataModel->where("id=".(int)$id)->find();
    		$result = $DataModel->delete((int)$id);
    		
    		if($result){
    			$root_path = dirname(dirname(dirname(dirname(__FILE__))));
    			unlink($root_path."/www".str_replace("http://".FRONT_SITE_NAME,"",$item['imgurl']));
    			header("Content-type: text/html; charset=utf-8");
    			echo "<script>layer.closeAll(); \$table.trigger(\"reloadGrid\");</script>";exit;
    		}
    	}
    }

    public function album_list(){
        $album_list= $this->get_img_files(dirname(dirname(dirname(dirname(__FILE__)))).'/www/Public/Uploads');

        foreach ($album_list as $key=>$value){
            $first= strpos($value, 'Public');
            $album_list[$key]=array(
			"url_a"=>'http://'.FRONT_SITE_NAME.'/'.substr_replace($value,'',0,$first),
			"url_b"=>'/'.substr_replace($value,'',0,$first)
			);
        }

        echo json_encode($album_list);

    }

    public function album_test(){
        $this->display();
    }


    public function get_img_files($path){
        $files=array();
        $queue=array($path);
        while($data=each($queue)){
            $path=$data['value'];
            if(is_dir($path) && $handle=opendir($path)){
                while($file=readdir($handle)){
                    if($file=='.'||$file=='..') continue;
                    $real_path=$path.'/'.$file;

                    if(preg_match("/\.(gif|jpeg|jpg|png|bmp)$/i", $file)  && preg_match("/^((?!_small).)*$/is", $file))
                        {
                        $files[] = $path.'/'.$file;
                    }

                    if (is_dir($real_path)) $queue[] = $real_path;
                }
            }
            closedir($handle);
        }
        return $files;
    }


}